package list;

import java.util.Scanner;

/**
 * https://blog.csdn.net/qq_40703471/article/details/124577811?spm=1001.2014.3001.5502
 */
public class RemoveNthFromEnd {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String s = sc.nextLine();
        String[] split = s.substring(1, s.length() - 1).split(",");

        int[] nums = new int[split.length];
        for (int i = 0; i < nums.length; i++) {
            nums[i] = Integer.parseInt(split[i]);
        }
        int n = sc.nextInt();
        ListNode list = ListUtil.getListFromArray(nums);
        ListNode out = removeNthFromEnd(list, n);

        ListUtil.listToString(out);
    }

    public static ListNode removeNthFromEnd(ListNode head, int n) {
        ListNode pre = new ListNode(-1);
        pre.next = head;
        ListNode left = pre;
        ListNode right = pre;
        //右指针先走n步
        while (n > 0) {
            right = right.next;
            n--;
        }
        //左右指针一起走
        while (right.next != null) {
            left = left.next;
            right = right.next;
        }
        //删除倒数第n个节点
        left.next = left.next.next;

        return pre.next;
    }
}
